﻿using AutoMapper;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.ProcessEgine.API.Applocations.Commands.GridProfessionalCommand;
using B.S.ProcessEgine.API.Dtos.ResponseDto;
using B.S.ProcessEgine.Domain.Entities;
using B.S.ProcessEgine.ErrorCode;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace B.S.ProcessEgine.API.Applocations.CommandHandlers.GridPeofessionalHandler
{
    public class ProFessionalListHandler:IRequestHandler<ProfessionalListCommand,ApiResult<IList<GridProfessionalRespDto>>>
    {
        IBaseRepository<GridProfessional> gridprofessionalrepository;
        ILogger<ProFessionalListHandler> logger;
        IMapper mapper;

        public ProFessionalListHandler(IBaseRepository<GridProfessional> gridprofessionalrepository, ILogger<ProFessionalListHandler> logger, IMapper mapper)
        {
            this.gridprofessionalrepository = gridprofessionalrepository;
            this.logger = logger;
            this.mapper = mapper;
        }

        public async Task<ApiResult<IList<GridProfessionalRespDto>>> Handle(ProfessionalListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await  gridprofessionalrepository.GetAll().Where(x=>!x.IsDel).ToListAsync(cancellationToken);

                var dto = mapper.Map<IList<GridProfessionalRespDto>>(list);

                return ApiResult<IList<GridProfessionalRespDto>>.Success(dto, StateCodeEnum.Ok);
            }
            catch (Exception ex) 
            {
                logger.LogError("表单类型列表出错" + ex.Message);
                throw;
            }
        }
    }
}
